<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Banner extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Bannermodel','m_banner',true);
    }

    public function index(){
        $data = parent::index();

        $data['attr'] = array();
        $data['attr']['banner_image_width'] = IMAGE_SIZE_BANNER_WIDTH;
        $data['attr']['banner_image_height'] = IMAGE_SIZE_BANNER_HEIGHT;

        $this->load->view('admin/banner',$data);
    }

    public function getBannerList(){
        $assist['where'] = getFilter($this->input->post(),Bannermodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Bannermodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Bannermodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        list($list,$count) = $this->m_banner->getBannerListSection($assist);

        foreach ($list as $k => $r) {
    			$list[$k]['banner_image'] = empty($r['banner_image']) ? '' : UPLOAD_URL_THUMB.$r['banner_image'];
    		}

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function createBanner(){
      $this->validate->setRule('banner_url','string');
      $this->validate->setRule('banner_url','notnull', false);
      $this->validate->setRule('banner_image','file');
      if(!$this->validate->run($this->input->post())){
        $this->response->setDetail($this->validate->getMessage());
        $this->response->responseExit();
      }
      $info = $this->validate->getResult();

      $info['banner_image'] = $this->m_app->saveFile($info['banner_image'],array(
        'width' => IMAGE_SIZE_BANNER_WIDTH,
        'height' => IMAGE_SIZE_BANNER_HEIGHT,
        'scale_width' => IMAGE_SIZE_DEFAULT_THUMB_WIDTH,
      ));

      $info['user_id'] = $this->m_app->getCurrentUserId();
      $info['banner_status'] = STATUS_PENDING;
      $info['banner_time_create'] = NOW;
      $this->m_banner->createBanner($info);


      $this->response->setSuccess();
      $this->response->responseExit();
    }

    public function editBanner(){
  		$this->validate->setRule('banner_id','integer');
      $this->validate->setRule('banner_url','string');
      $this->validate->setRule('banner_url','notnull', false);
      $this->validate->setRule('banner_image','file');
  		$this->validate->setRule('banner_image','optional');
  		if(!$this->validate->run($this->input->post())){
  			$this->response->setDetail($this->validate->getMessage());
  			$this->response->responseExit();
  		}
  		$info = $this->validate->getResult();

  		if(!empty($info['banner_image'])){
  			$info['banner_image'] = $this->m_app->saveFile($info['banner_image'],array(
          'width' => IMAGE_SIZE_BANNER_WIDTH,
          'height' => IMAGE_SIZE_BANNER_HEIGHT,
          'scale_width' => IMAGE_SIZE_DEFAULT_THUMB_WIDTH,
  			));
  		}

  		$info['user_id'] = $this->m_app->getCurrentUserId();
  		$this->m_banner->editBanner($info);


  		$this->response->setSuccess();
  		$this->response->responseExit();
  	}


	public function disableBanner(){
		$this->validate->setRule('banner_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['banner_status'] = STATUS_DISABLE;

		$this->m_banner->editBanner($info);


		$this->response->setSuccess();
		$this->response->responseExit();
	}

	public function activeBanner(){
		$this->validate->setRule('banner_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_id'] = $this->m_app->getCurrentUserId();
		$info['banner_status'] = STATUS_ACTIVE;

		$this->m_banner->editBanner($info);


		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

  public function deleteBanner(){
		$this->validate->setRule('banner_id','integer');
    if(!$this->validate->run($this->input->post())){
      $this->response->setDetail($this->validate->getMessage());
      $this->response->responseExit();
    }
    $info = $this->validate->getResult();
    $info['user_id'] = $this->m_app->getCurrentUserId();
    $info['banner_status'] = STATUS_DELETE;

    $this->m_banner->editBanner($info);


    $this->response->setSuccess();
    $this->response->responseExit();
  }

  public function editBannerSort(){
    $this->validate->setRule('banner_id','integer');
    $this->validate->setRule('banner_sort','integer');
    if(!$this->validate->run($this->input->post())){
      $this->response->setDetail($this->validate->getMessage());
      $this->response->responseExit();
    }
    $info = $this->validate->getResult();

    $this->load->database();
    $this->m_banner->editBanner(array(
      'banner_id' => $info['banner_id'],
      'user_id' => $this->m_app->getCurrentUserId(),
      'banner_sort' => $info['banner_sort'],
    ));
    $this->db->close();

    $this->response->setSuccess();
    $this->response->responseExit();
  }
}
/* End of file api_log.php */
/* Location: ./application/controllers/api_log.php */
